//
//  FObject.m
//  test
//
//  Created by 袁飞 on 12-5-29.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FObject.h"
#import "TBXML.h"
//#import "FFlashImg.h"
#import "SimpleAudioEngine.h"
#import "FUtil.h"
/*
<fobject>
  <compant>
    <name>test</name>
    <sprite>
      <position></position>
      <img>a.png</img>
    </sprite>
  </compant>
</fobject>
*/
@implementation FObject
@synthesize name = name_;

+(FObject*)FObjectFromImg:(NSString*)name XY:(CGPoint)xy Count:(int)count{
  
}
+(FObject*)FObjectFromXmlFile:(NSString*)name WithDict:(NSMutableDictionary*)dict{
  FObject *obj = [[[FObject alloc] init] autorelease];
  [obj initWithXml:name withDict:dict];
  return obj;
}

//-(id)initWithImg:(FFlashImg*)img{
//}
-(id)initWithXml:(NSString*)file withDict:(NSMutableDictionary*)dict{
  [super init];
  compants_sprite_ = [NSMutableDictionary dictionary];
  [compants_sprite_ retain];
  actions_ = [NSMutableDictionary dictionary];
  [actions_ retain];
  //NSLog(@"load xml %@", file);
  TBXML *xml = [[TBXML alloc] initWithXMLFile:file];
  if (!xml) {
    NSLog(@"load xml err %@", file);
  }
  TBXMLElement *root_element = [xml rootXMLElement];
  NSMutableDictionary* tmp = [FUtil getDictFromAttribute:root_element];
  if (dict != nil) {
    for(NSString* s in dict){
      [tmp setObject:[dict objectForKey:s] forKey:s];
    }
  }
  dict = tmp;
  [xml release];
  NSString* fullfilename = [[CCFileUtils sharedFileUtils] fullPathFromRelativePath:file];
  NSString *data = [NSString stringWithContentsOfFile:fullfilename encoding:NSUTF8StringEncoding error:nil];
  NSString *res = [FUtil expandStr:data WithDict:dict];
  
  xml = [[TBXML alloc] initWithXMLString:res error:nil];
  root_element = [xml rootXMLElement];
  CGSize s = [FUtil getSize:root_element];
  //[l setContentSize:s];
  CGPoint p = [FUtil getPosition:root_element];
  [self setPosition:p];

  TBXMLElement *element = root_element->firstChild;
  for(;element!=nil;){
    NSString *name = [TBXML elementName:element];
    //NSLog(@"node %@", name);
    if([name compare:@"sprite"] == NSOrderedSame){
      NSString *tn = [FUtil getName:element];
      NSString *img = [FUtil getImg:element];
      CCSprite *sp = [CCSprite spriteWithSpriteFrameName:img];
      [compants_sprite_ setObject:sp forKey:tn];
      [self addChild:sp];
    }else if([name compare:@"action"] == NSOrderedSame){
      NSString *tn = [FUtil getName:element];
      NSMutableDictionary* td = [FUtil getDictFromAttribute:element];
      CCAction* action = [FUtil actionWith:[td objectForKey:@"file"] WithDict:td];
      [actions_ setObject:action forKey:tn];
    }
    element = element->nextSibling;
  }
  return self;
}

-(void)startAi{
  [ai_ start];
}

-(void)moveTo:(CGPoint)p Dur:(float)dur{
  CCMoveTo *ma = [CCMoveTo actionWithDuration:2.0 position:p];
  CCCallFunc* ca = [CCCallFunc actionWithTarget:ai_ selector:@selector(onMoveEnd)];
  CCAction *ac = [CCSequence actions:ma,ca,nil];
  [self runAction:ac];
}

-(CCSprite*)getSpriteByName:(NSString*)name{
  return [compants_sprite_ objectForKey:name];
}

-(CCSprite*)setSpriteByName:(NSString*)name{
  
}

-(void)runAction:(NSString*)name WithSprite:(NSString*)spritename{
  CCSprite* sp = [compants_sprite_ objectForKey:spritename];
  [sp runAction:[actions_ objectForKey:name]];
}

-(void)runAction:(NSString*)name WithSprite:(NSString*)spritename Dur:(float)dur{
  CCSprite *sp = [compants_sprite_ objectForKey:spritename];
  CCAction *at = [actions_ objectForKey:name];
  //CCCallFunc* ca = [CCCallFunc actionWithTarget:ai_ selector:@selector(on)];
  CCCallFuncND *cfnd = [CCCallFuncND actionWithTarget:ai_ selector:@selector(onActionEnd:Data:) data:name];
  CCAction *ac = [CCSequence actions:at,cfnd,nil];
  [sp runAction:ac];
}

-(void)runActionRepeated:(NSString*)name WithSprite:(NSString*)spritename{
  CCSprite* sp = [compants_sprite_ objectForKey:spritename];
  CCAction *at = [actions_ objectForKey:name];
  CCAction *ac = [CCRepeatForever actionWithAction:at];
  [sp runAction:ac];
}

-(void)stopAction:(NSString*)name WithSprite:(NSString*)spritename{
  CCSprite* sp = [compants_sprite_ objectForKey:spritename];
  CCAction *at = [actions_ objectForKey:name];
  [sp stopAction:at];
}

-(void)stopAllAction:(NSString*)spritename{
  CCSprite* sp = [compants_sprite_ objectForKey:spritename];
  [sp stopAllActions];
}

-(void)setAi:(FAI*)ai{
  ai_ = ai;
  [ai_ retain];
}
@end
